package com.asset.dispoal.file.evidenceRecognition.service.commom;

import com.asset.dispoal.file.evidenceRecognition.dto.EvidenceRecognitionRequest;
import com.asset.dispoal.file.evidenceRecognition.dto.SingleEvidenceRecognitionRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.exception.BusinessException;
import tech.mhuang.pacebox.core.file.FileUtil;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.io.IOUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;


/**
 * @author qr
 * @date 2021/11/23$
 */
@Component
@Slf4j
public class SingleEvidenceUploadCommand implements BaseCommand<SingleEvidenceRecognitionRequest, Result> {

    @Autowired
    BaseIdeable<String> snowFlake;

    @Autowired
    private Environment environment;

    @Autowired
    FileEvidenceRecognitionCommand fileEvidenceRecognitionCommand;

    @Override
    public Result execute(SingleEvidenceRecognitionRequest request) {
        LocalDate now = LocalDate.now();
        String year = String.valueOf(now.getYear());
        String month = String.valueOf(now.getMonthValue());
        String day = String.valueOf(now.getDayOfMonth());
        String generateId = snowFlake.generateId();
        //系统定义上传文件解析后存放的路径
        String sysUploadUrl = environment.getProperty("file.evidence.url");

        String filePath = request.getEvidenceTypeId() + File.separator + request.getAssetInstBatchId() + File.separator;
        String resolveUrl = File.separator + year + File.separator + month + File.separator + day + File.separator + filePath;
        String baseResolveUrl = sysUploadUrl + resolveUrl;
        try {
            //保存源文件的上传路径以及原名称
            if (!new File(baseResolveUrl).exists()) {
                FileUtil.createDirectory(baseResolveUrl, true);
            }
            String replaceFileName = generateId + "_" + request.getFile().getOriginalFilename();
            FileOutputStream resolveFs = new FileOutputStream(baseResolveUrl + replaceFileName);
            IOUtil.copy(request.getFile().getInputStream(), resolveFs, true);
            EvidenceRecognitionRequest recognitionRequest = new EvidenceRecognitionRequest();
            recognitionRequest.setRecognitionType("4");
            recognitionRequest.setAssetInstId(request.getAssetInstId());
            recognitionRequest.setAssetInstBatchId(request.getAssetInstBatchId());
            recognitionRequest.setEvidenceTypeId(request.getEvidenceTypeId());
            recognitionRequest.setContractId(request.getContractId());
            recognitionRequest.setFilepath(resolveUrl);
            recognitionRequest.setReplaceFileName(replaceFileName);
            recognitionRequest.setOriginalName(request.getFile().getOriginalFilename());
            fileEvidenceRecognitionCommand.execute(recognitionRequest);
        } catch (IOException e) {
            log.error("保存文件异常", e);
            throw new BusinessException(500, e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(500, e.getMessage(), e);
        }
        return Result.ok();
    }
}
